import datetime
import threading
import sys

debug = False
normal = True
outfile = None
#outfile = open("/tmp/log.txt", "w")

def log(*args):
  try:
    s = " ".join(map(lambda x: unicode(x), args))
    msg = u'%s%s\n' % (Header(), s)
    if normal:
      sys.stdout.write(msg)
    if outfile:
      outfile.write(msg)
      outfile.flush()
  except:
    print "-- Unloggable message --"

def logdebug(*args):
  if debug:
    log(*args)

def Header():
  now = datetime.datetime.now()
  return u"%d-%d %02d:%02d:%02d %s: " % (now.month, now.day, now.hour,
      now.minute, now.second, threading.currentThread().getName())
